package com.example.uiclassassignments.Activity;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.uiclassassignments.R;
import com.example.uiclassassignments.tools.FragmentViewPager1;
import com.example.uiclassassignments.tools.FragmentViewPager2;
import com.example.uiclassassignments.tools.FragmentViewPager3;

import java.util.ArrayList;
import java.util.List;

public class FragmentViewPagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
    ViewPager viewPager;
    RadioGroup radioGroup;
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_view_pager);
        viewPager=findViewById(R.id.pager);
        radioGroup=findViewById(R.id.rg);
        initData();
    }
    List<Fragment> data;
    protected void initData() {
        data = new ArrayList<Fragment>();
        data.add(new FragmentViewPager1());
        data.add(new FragmentViewPager2());
        data.add(new FragmentViewPager3());
        FragmentPagerAdapter adapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return data.get(i);
            }

            @Override
            public int getCount() {
                return data.size();
            }
        };
        viewPager.setAdapter(adapter);
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        viewPager.setOnPageChangeListener(this);
    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        for (int z = 0; z < radioGroup.getChildCount(); z++) {
            ((RadioButton) radioGroup.getChildAt(z)).setChecked(z == i);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
